import { getGetregionlist } from "@/api/siderBar/home";
import type { Region } from "@/api/siderBar/model";
import type { Options } from "vue-request";

interface UseFetchAreasOpts extends Omit<Options<Region[], []>, "cacheKey" | "staleTime" | "setCache" | "getCache"> {
}
const defCacheTime = 24 * 60 * 60 * 1000
export function useFetchAreas(opts?: UseFetchAreasOpts) {
    const { cacheTime = defCacheTime, ..._opts } = opts ?? {};
    const cacheKey = getLsKey('getGetregionlist')
    const { data, loading, refreshAsync } = useRequest(() => getGetregionlist(), {
        cacheKey,
        staleTime: cacheTime,
        setCache: (cacheKey, data) => {
            localStorage.setItem(cacheKey, JSON.stringify(data))
        },
        getCache: cacheKey => {
            const cached = localStorage.getItem(cacheKey)
            if (cached) {
                return JSON.parse(cached)
            }
        },
        ..._opts,
    })
    function clearCache() {
        localStorage.removeItem(cacheKey)
    }
    return {
        data,
        loading,
        refreshAsync,
        clearCache
    }
}